<template>
  <div class="main-all">
    <p> <i class="el-icon-place"></i>专业管理</p>
    <div class="main">
      <div class="main-1">
        <el-button icon="el-icon-plus" @click="dialogFormVisible = true; ADD()">新建</el-button>
        <div style="display: flex;">
          <el-input v-model="inputZ" placeholder="专业名"></el-input>
          <el-input v-model="inputX" placeholder="系名"></el-input>
          <el-button type="primary" @click="getPage" icon="el-icon-search">搜索</el-button>
        </div>
      </div>
      <el-pagination @current-change="fn2" :current-change="page" background layout="prev, pager, next" :total="total"
        style="margin-bottom: 10px;">
        <!--                                          total页码，设定为变量 -->
      </el-pagination>
      <div style="overflow-y: scroll;width: 100%;height: 80%;">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="专业ID">
        </el-table-column>
        <el-table-column prop="name" label="专业名">
        </el-table-column>
        <el-table-column prop="departmentName" label="所属系">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click=" dialogFormVisible = true; changed(scope.row)">编辑</el-button>
            <el-button type="text" @click="  deletes(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <!--  -->


      <el-dialog width="35%" title="编辑" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="专业名" :label-width="formLabelWidth">
            <el-input style="width: 80%; margin-left: 0;" v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="所属系" :label-width="formLabelWidth">
            <el-select style="width: 55%; margin-left: -30%;" v-model="form.region" placeholder="请选择系">
              <el-option v-for="(item, index) in tableDataAll" :key="index" :label="item.name"
                :value="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false; ADDorChange()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>


</template>

<script>
import serve from '../api/index.js'
import { getListAll, getPages, getList, ADDList, changeorno, deleted } from '../api/major.js'
export default {
  data() {
    return {
      inputZ: '',
      inputX: '',
      total: 100,
      page: 1,
      tableData: [],
      tableDataAll: [],
      dialogFormVisible: false,
      form: {
        name: '',    //新建专业名
        region: '',  //所属系
        departmentId: ''
      },
      formLabelWidth: '100px',
      OR: ''
    }
  },
  methods: {
    getPage() {
      getPages(this.inputZ, this.inputX).then((res) => {
        console.log(res.data);
        this.total = res.data * 10
        if (res.message == 'success') {
          getList(this.inputZ, this.inputX, this.page).then((res) => {
            console.log(res);
            this.tableData = res.data
          })
          getListAll().then((res) => {
            this.tableDataAll = res.data
            console.log(this.tableDataAll);

          })
        }
      })
    },//获取列表数据
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },//element
    fn2(val) {
      this.page = `${val}`
      console.log(this.page);
      this.getPage()
    },//获取页数

    fn() {
      // 获取 departmentId
      this.tableDataAll.filter((item) => {
        if (item.name == this.form.region) {
          this.form.departmentId = item.id
        }
      })
      // 获取ID
      this.tableData.filter((item) => {
        if (item.name == this.form.name) {
          this.form.id = item.id
        }
      })
    },
    ADDorChange() {
      if (this.OR == 'ADD') {
        this.fn()
        ADDList(this.form.name, this.form.departmentId).then((res) => {
          console.log(res);
          this.getPage()
        })
      } else if (this.OR == 'changed') {
        this.fn()
        changeorno(this.form.name, this.form.departmentId, this.form.id).then((res) => {
          console.log(res);
          this.getPage()
        })
      }
    },
    ADD() {
      this.form = {
        name: '',
        region: '',
        departmentId: ''
      },
        this.OR = 'ADD'
      console.log(this.form);
    },//添加列表数据  √

    changed(e) {
      this.OR = 'changed'
      console.log(e);
      this.form.name = e.name
      this.form.region = e.departmentName
    }, //修改列表数据

    deletes(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(id).then((res) => {
          console.log(res);
        })
        this.getPage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      }); //删除列表数据   xxxx未完成

    }

  },
  mounted() {
    this.getPage()  //初始化列表数据
  }
}

</script>

<style scoped>
.el-table {
  width: 100% !important;
}

.main {
  .el-button {
    height: 40px;
  }

  .main-1 {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 10px;
    justify-content: space-between;
  }

  .el-input {
    height: 30px;
    width: 100px;
    margin-right: 20px;
    margin-bottom: 10px;
  }

}
</style>